package de.tabuma.lib.cm.crud;

import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.Content;
import javax.servlet.http.*;
import java.lang.reflect.*;
import de.tabuma.lib.*;
import java.util.*;
import java.util.logging.*;

/**
 *
 * @author alefen
 */
public class CreateViewFacade {
    private static final Logger log = Logger.getLogger(CreateViewFacade.class.getName());
    
    public static String view(Content content, HttpServletRequest request) {

        String cancel = "cancel";
        String create = "create";

        String str = "";
        str += "<div class=\"contentContent\">";
        str += view.getFormBeginn(content.GeneralContent.getServlet(), null);

        if(request.getAttribute(constants.MESSAGE_ERROR)!=null) {
            str += "<div class=\"errormessage\"><div class=\"errormessage2\">"+request.getAttribute(constants.MESSAGE_ERROR)+"</div></div>\n";
        }
        if(request.getAttribute(constants.MESSAGE_INFO)!=null) {
            str += "<div class=\"infomessage\"><div class=\"infomessage2\">"+request.getAttribute(constants.MESSAGE_INFO)+"</div></div>\n";
        }

        str += "<div class=\"cols\">";
        for(int i=0;i<content.FormContent.getEditableFields().size();i++) {
            Field myField = content.FormContent.getEditableFields().get(i);
            Integer type = content.FormContent.getEditableFieldTypes().get(i);
            String displayedName = content.FormContent.getDisplayedName().get(myField);
            int printType = 0;
            switch(type.intValue()) {
                case constants.TYPE_LIST:
                    printType = view.TYPE_LIST;
                    break;
                case constants.TYPE_LIST_STRING:
                    printType = view.TYPE_LIST_STRING;
                    break;
                case constants.TYPE_PASSWORD:
                    printType = view.TYPE_PASSWORD;

                    break;
                case constants.TYPE_STRING:
                    printType = view.TYPE_FIELD;
                    break;
                case constants.TYPE_TEXTAREA:
                    printType = view.TYPE_TEXTAREA;
                    break;
                case constants.TYPE_BOOLEAN:
                    printType = view.TYPE_CHECKBOX;
                    break;
                default:
                    printType = view.TYPE_FIELD;
                    break;
            }

            String labelClass = null;
            String fieldClass = null;
            if(request.getAttribute(constants.ERROR_FIELD+myField.getName())!=null) {
                labelClass = "error";
                fieldClass = "errorfield";
            }

            if(i%2==0) {

                str += view.printForm1(displayedName, myField.getName(), "", printType, content.ListContent.getListParameterContent().get(myField), content.ListContent.getListParameterDisplayedField().get(myField), labelClass, fieldClass);
            }
            else {
                str += view.printForm2(displayedName, myField.getName(), "", printType, content.ListContent.getListParameterContent().get(myField), content.ListContent.getListParameterDisplayedField().get(myField), labelClass, fieldClass);
            }
        }
        if(content.FormContent.getEditableFields().size()%2==1) {
            str += "<div class=\"col2\">\n";
            str += "<div class=\"col2_head\"></div>\n";
            str += "<div class=\"col2_content\"></div>\n";
            str += "</div>\n";
        }
        
        str += "</div>";
        str += view.getHiddenAsAttribute(content.GeneralContent.getParameter());
        str += view.printHiddenField(content.GeneralContent.getParamName(), ""+constants.CREATE);
        str += view.printHiddenField(constants.CREATE1, "true");
        str += "<input type=\"submit\" name=\"submit\" class=\"button\" value=\""+create+" "+content.FormContent.getEntityName()+"\"/>";
        str += "<input type=\"submit\" name=\"" + constants.CANCEL + "\" class=\"button\" value=\""+cancel+"\"/>";
        str += view.getFormEnd();
        str += "</div>";
        return str;
    }
}